package com.effectiv.gooruda.rest.api.controller;


import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;


import com.effectiv.gooruda.rest.api.domain.Cache;
import com.effectiv.gooruda.rest.api.domain.CacheEntry;
import com.effectiv.gooruda.rest.api.operation.CacheOperation;

@Controller
@RequestMapping("/cache")
@Slf4j
public class CacheController implements CacheOperation{
	
	@Autowired
	@Qualifier("cacheServiceImpl")
	private CacheOperation cacheServiceOperation;
	
	
	
	@RequestMapping(value="/{cacheName}" , method=RequestMethod.GET)
	public @ResponseBody Cache findOne(@PathVariable String cacheName){
		return cacheServiceOperation.findOne(cacheName);
	}
	
	@RequestMapping(value="/{cacheName}/key/{key}" , method=RequestMethod.POST)
	@ResponseStatus(value=HttpStatus.CREATED)
	public void addEntry(@PathVariable String cacheName,@PathVariable String key,  @RequestBody  CacheEntry entry){
		log.info("Adding an entry to cache.");
		log.info("cacheName = {}", cacheName);
		log.info("key = {}", key);
		log.info("entry = {}", entry);
		log.info("entry = {}", entry.getClass().getName());
		
		cacheServiceOperation.addEntry(cacheName, key, entry);
	}
	
	@RequestMapping(value="/{cacheName}/key/{key}" , method=RequestMethod.GET)
	public @ResponseBody CacheEntry getEntry(@PathVariable String cacheName,@PathVariable  String key){
		log.info("Getting an entry from cache.");
		log.info("cacheName = {}", cacheName);
		log.info("key = {}", key);
		
		return cacheServiceOperation.getEntry(cacheName, key);
	}
	
	@RequestMapping(value="/{cacheName}" , method=RequestMethod.DELETE)
	@ResponseStatus(value=HttpStatus.OK)
	public @ResponseBody Cache delete(@PathVariable String cacheName){
		return cacheServiceOperation.delete(cacheName);
	}
	
	
	
	@RequestMapping(value="/{cacheName}/clear" , method=RequestMethod.DELETE)
	public void clear(@PathVariable String cacheName){
		cacheServiceOperation.clear(cacheName);		
	}
	
	@RequestMapping(value="/{cacheName}/key/{key}/remove" , method=RequestMethod.DELETE)
	public @ResponseBody CacheEntry removeEntry(@PathVariable String cacheName,@PathVariable String key){
		return cacheServiceOperation.removeEntry(cacheName, key);
	}
	
	@RequestMapping(value="/{cacheName}/key/{key}/increment" , method=RequestMethod.POST)
	public @ResponseBody CacheEntry increment(@PathVariable String cacheName,@PathVariable String key){
		log.info("increment an entry in cache.");
		log.info("cacheName = {}", cacheName);
		log.info("key = {}", key);
		return cacheServiceOperation.increment(cacheName, key);
	}
	/*
	public List<CacheEntry> findAll(String cacheName, String selector){
		return null;
	}
	
	@RequestMapping(value="/list" , method=RequestMethod.GET)
	public @ResponseBody List<Cache> listCaches(){
		
		return cacheServiceOperation.listCaches();
	}
	*/
}
